package com.jbm.le;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.jbm.onehome.iface.IEGuardKey;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created by Sun on 2016/11/9.
 */

public class BTDoorCtrl extends Resource
{
    private final static String TAG = JBMBLEEGuard.class.getSimpleName();
    static boolean isbind = false;
    protected BlockingQueue m_once = new ArrayBlockingQueue(1);

    public BTDoorCtrl()
    {
        m_once.offer(1);
    }

    public class LocalBinder extends Binder
    {
        BTDoorCtrl getService() {
            return BTDoorCtrl.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // After using a given device, you should make sure that BluetoothGatt.close() is called
        // such that resources are cleaned up properly.  In this particular example, close() is
        // invoked when the UI is disconnected from the Service.
        return super.onUnbind(intent);
    }

    private final IBinder mBinder = new LocalBinder();

    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothManager mBluetoothManager = null;
    private BluetoothAdapter mBluetoothAdapter = null;

    public boolean initialize()
    {
        // For API level 18 and above, get a reference to BluetoothAdapter through
        // BluetoothManager.

        if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            if (mBluetoothManager == null) {
                Log.e(TAG, "Unable to initialize BluetoothManager.");
                return false;
            }
        }

        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
            return false;
        }

        return true;
    }

    public boolean connect(final IEGuardKey key, final DoorEvent doorevent, String sname, int trytimes, int timeout )
    {
        if (mBluetoothAdapter == null || key == null)
        {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            if ( doorevent != null )
                doorevent.OtherError("蓝牙设备初始化失败");
            return false;
        }

        BTKeyGattCallback callback = new BTKeyGattCallback();

/*
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(key.mac);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }else
        {
            SysLog.g_syslog.MakeLog(SysLog.SYS_LOG_LEVEL_DEBUG, "%s first connect to %s rs=%d", sname, key.mac, resource.size());
            callback.Init(key, doorevent, sname);
            callback.mBluetoothGatt = device.connectGatt(this, false, callback);
            if (callback.mBluetoothGatt != null)
            {
                callback.Born(trytimes, timeout);
                Add(callback);
                SysLog.g_syslog.MakeLog(SysLog.SYS_LOG_LEVEL_DEBUG, "%s first connected %s rs=%d ln=%s", sname, key.mac, resource.size(), callback.GetLifeName().toString());
            } else
                SysLog.g_syslog.MakeLog(SysLog.SYS_LOG_LEVEL_DEBUG, "%s first connect failed %s rs=%d", sname, key.mac, resource.size());
        }
        return true;
*/
        callback.Init(key,doorevent,sname);
        return callback.Open(mBluetoothAdapter,trytimes,timeout,this,false);
    }
}
